#include "stdafx.h"
/// Copyright 2012, Alex Averian, Yonan Research
/// Released under BSD Licence



#include "qclipboard.h"
using namespace micro::core;


QClipboard::QClipboard( HWND h )
{
    m_hWnd = h;
}

QClipboard::~QClipboard()
{

}


LPTSTR  QClipboard::GetText()
{
    if ( FALSE == OpenClipboard ( m_hWnd ) )
        return nullptr;

    HANDLE hMem = GetClipboardData ( CF_UNICODETEXT );

    if ( hMem == nullptr )
    {
        CloseClipboard ();
        return nullptr;
    }

    LPCSTR pszText = ( LPCSTR ) GlobalLock ( hMem );
    m_strText = pszText;
    GlobalUnlock ( hMem );

    CloseClipboard ();

    return m_strText;
}

bool QClipboard::SetText( LPTSTR psz )
{
    size_t len = _tcslen ( psz );
    size_t sz = ( len + 1 ) * sizeof( TCHAR );
    HANDLE hText = GlobalAlloc ( GHND | GMEM_SHARE,  sz  );

    if ( nullptr == hText )
        return false;

    LPTSTR pszText = ( LPTSTR ) GlobalLock ( hText );
    _tcscpy_s ( pszText, len + 1,  psz );
    GlobalUnlock ( hText );


    if ( FALSE == OpenClipboard ( m_hWnd ) )		return false;

    EmptyClipboard ();
    SetClipboardData ( CF_UNICODETEXT, hText );

    CloseClipboard ();

    return true;
}
